from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait


def xjzfcg(url):
    options = Options()
    options.binary_location = "seleniumtools/chrome-linux/chrome"
    # options.add_argument('--headless') # 不显示GUI
    options.add_argument("--kiosk-printing")  # 静默打印，无需用户点击打印页面的确定按钮
    service = Service("seleniumtools/chromedriver_linux64/chromedriver")
    with webdriver.Chrome(options=options, service=service) as driver:
        driver.implicitly_wait(1)
        driver.maximize_window()
        driver.get(url)
        WebDriverWait(driver, timeout=1)
        driver.find_element(
            By.XPATH, '//*[@id="floor-1709189989843072"]/div/div/ul/a[4]/li'
        ).click()
        # 如何遍历所有分页
        while (
                driver.find_element(
                    By.XPATH,
                    '//*[@id="floor-1678260517723048"]/div/div[2]/div[2]/div[2]/div/div[2]/div/button[2]',
                ).is_enabled()
                is False
        ):
            pass
        print(
            "总记录数："
            + driver.find_element(
                By.XPATH,
                '//*[@id="floor-1678260517723048"]/div/div[2]/div[2]/div[2]/div/div[2]/span',
            ).text
        )
        # 切换分页 为每页100条记录
        # driver.find_element(By.XPATH,'//*[@id="floor-1678260517723048"]/div/div[2]/div[2]/div[2]/div/div[2]/div/span[1]/div/div/span/span/i').click()
        # driver.find_element(By.XPATH,'/html/body/div[3]/div/div[1]/ul/li[3]').click()
        # input("按任意键退出...")

        # 遍历前600页
        for p in range(10):
            print(
                "-------------------------- 第"
                + str(p + 1)
                + "页 --------------------------"
            )
            driver.find_element(
                By.XPATH,
                '//*[@id="floor-1678260517723048"]/div/div[2]/div[2]/div[2]/div/div[1]/ul',
            ).screenshot("./last_page.png")

            # 处理当前页
            page = driver.find_element(
                By.XPATH,
                '//*[@id="floor-1678260517723048"]/div/div[2]/div[2]/div[2]/div/div[1]/ul',
            )
            elements = page.find_elements(By.TAG_NAME, "li")
            element_count = len(elements)
            print(f"本页有{element_count}个记录")
            for i in range(element_count):
                # 分页中的每条记录,点击会打开新标签页
                print(
                    elements[i]
                    .text.replace(" ", "")
                    .replace("\n", "")
                    .replace("\r", "")
                )
                driver.find_element(
                    By.XPATH,
                    '//*[@id="floor-1678260517723048"]/div/div[2]/div[2]/div[2]/div/div[1]/ul/li['
                    + str(i + 1)
                    + "]/a",
                ).click()
                # elements[i].find_element(By.TAG_NAME, 'a').click()
                # print(driver.window_handles)
                driver.switch_to.window(driver.window_handles[1])
                # print('当前标签页 ' + driver.current_window_handle)

                # 下载pdf,打印功能无法使用隐藏GUI模式
                driver.find_element(
                    By.XPATH,
                    '//*[@id="floor-1678439968192029"]/div/div[1]/div[2]/div[1]/span',
                ).click()
                print("下载中...")
                # 关闭标签页
                driver.close()
                driver.switch_to.window(driver.window_handles[0])
                # print('当前标签页 ' + driver.current_window_handle)

            # 点击下一页
            if (
                    driver.find_element(
                        By.XPATH,
                        '//*[@id="floor-1678260517723048"]/div/div[2]/div[2]/div[2]/div/div[2]/div/button[2]',
                    ).is_enabled()
                    is True
            ):
                driver.find_element(
                    By.XPATH,
                    '//*[@id="floor-1678260517723048"]/div/div[2]/div[2]/div[2]/div/div[2]/div/button[2]',
                ).click()


if __name__ == "__main__":
    xjzfcg("http://x.x.x.x/")
